package com.baiyang.server.control.system;

import java.util.List;
import java.util.Map;

import com.baiyang.server.model.system.MenuRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import com.baiyang.server.annotation.Jurisdiction;
import com.baiyang.server.model.PageEntity;
import com.baiyang.server.model.system.Role;
import com.baiyang.server.service.system.RoleService;
import com.baiyang.server.tools.DataTool;
import com.baiyang.server.tools.Letter;
import com.baiyang.server.tools.SubjectNotNull;

@RestController
@RequestMapping(value = "/roleOperation")
@Jurisdiction(value = "角色信息")
public class RoleCon {
	@Autowired
	private RoleService roleService = null;
	
	/**
	 * 根绝条件分页获取数据
	 * @return
	 */
	@Jurisdiction(value = "批量读取")
	@RequestMapping(value = "/getPage",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Role> getPage(@RequestBody JSONObject object){
		Letter<Role> letter = new Letter<Role>();
		try {
			PageEntity<Role> result = roleService.getPage(object);
			letter.setList(result.getDatas());
			letter.setTotal(result.getCount());
			letter.setDetail(result.getResult());
			letter.setResult("SUCCESS");
			letter.setDetail("数据获取成功！");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}
	
	
	
	
	/**
	 * 根据条件获取一个用户信息
	 * @param role
	 * @return
	 */
	@Jurisdiction(value = "读取")
	@RequestMapping(value = "/selectOne",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Role> selectOne(@RequestBody Role role){
		Letter<Role> letter = new Letter<Role>();
		try {
			Role obj = roleService.selectOne(role);
			letter.setData(obj);
			letter.setResult("SUCCESS");
			letter.setDetail("数据获取成功！");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	@Jurisdiction(value = "获取所有角色信息")
	@RequestMapping(value = "/selectAll",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Role> selectAll(){
		Letter<Role> letter = new Letter<Role>();
		try {
			Role role = new Role();
			List<Role> obj = roleService.selectList(role);
			letter.setList(obj);
			letter.setResult("SUCCESS");
			letter.setDetail("数据获取成功！");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}
	
	/**
	 * 插入数据
	 * @param role
	 * @return
	 */
	@Jurisdiction(value = "新增")
	@RequestMapping(value = "/insert",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insert(@RequestBody Role role){
		Letter<String> letter = new Letter<String>();
		role.setId(DataTool.createUUID());
		Map<String, String> sub = SubjectNotNull.subject(role);
		if(sub.size() != 0) {
			letter.setResult("FAIL");
			letter.setDetail("必填信息不能为空！");
			return letter;
		}
		try {
			roleService.insert(role);
			letter.setDetail("数据新增成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}
	
	/**
	 * 批量插入数据
	 * @param roles
	 * @return
	 */
	@Jurisdiction(value = "批量新增")
	@RequestMapping(value = "/insertBatch",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insertBatch(@RequestBody List<Role> roles){
		Letter<String> letter = new Letter<String>();
		try {
			roleService.insertBatch(roles,letter);
			letter.setDetail("SUCCESS");
			letter.setResult("数据新增失败");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}
	
	/**
	 * 根绝条件进行删除
	 * @param role
	 * @return
	 */
	@Jurisdiction(value = "删除")
	@RequestMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> delete(@RequestBody Role role){
		Letter<String> letter = new Letter<String>();
		if(SubjectNotNull.subjectIsNull(role)) {
			letter.setResult("FAIL");
			letter.setDetail("信息不能为空！");
			return letter;
		}
		try {
			roleService.delete(role);
			letter.setDetail("数据删除成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 根绝条件进行删除
	 *
	 * @param roles
	 * @return
	 */
	@Jurisdiction(value = "deletes")
	@RequestMapping(value = "/deletes", produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> deletes(@RequestBody List<Role> roles) {
		Letter<String> letter = new Letter<String>();
		if (DataTool.isBlank(roles)) {
			letter.setResult("FAIL");
			letter.setDetail("请至少选中一行！");
			return letter;
		}
		try {
			roleService.deletes(roles);
			letter.setDetail("数据删除成功");
			letter.setResult("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据删除成功！");
		}
		return letter;
	}

	/**
	 * 根绝id更新
	 * @param role
	 * @return
	 */
	@Jurisdiction(value = "修改")
	@RequestMapping(value = "/updateById",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> updateById(@RequestBody Role role){
		Letter<String> letter = new Letter<String>();
		if(role.getId().equals("")) {
			letter.setResult("FAIL");
			letter.setDetail("id不能为空！");
			return letter;
		}
		try {
			roleService.updateById(role);
			letter.setDetail("数据修改成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据修改失败");
		}
		return letter;	
	}
	
	/**
	 * @apiNote 初始化角色信息，创建超级管理员角色
	 * @author XVX
	 */
	@Jurisdiction(value = "初始化角色信息")
	@RequestMapping(value = "/initRole",produces = MediaType.APPLICATION_JSON_VALUE)
	public void initRole(){
		Role role = new Role();
		role.setId(DataTool.createUUID());
		role.setRoleName("超级系统管理员");
		role.setCreateBy("auto_baiyang");
		roleService.insert(role);
	}
}
